import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _class, _class2;

/**
 * 当详情页展示的公告信息时, userMessageId 时 noticeId
 * userMessage 站内消息详情
 * @date: 2018-8-4

 */
import React, { PureComponent } from 'react';
import { Spin, Divider } from 'antd';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import { Content, Header } from 'components/Page';
import UploadModal from 'components/Upload';
import { getCurrentOrganizationId } from 'utils/utils';
import { dateTimeRender } from 'utils/renderer';
import { BKT_MSG } from 'utils/config';
import intl from 'utils/intl';
import styles from "../index.less?modules";
var BadgeIcon = (_dec = connect(function (_ref) {
  var userMessage = _ref.userMessage,
      loading = _ref.loading;
  return {
    userMessage: userMessage,
    organizationId: getCurrentOrganizationId(),
    queryDetailLoading: loading.effects['userMessage/queryDetail']
  };
}), _dec2 = Bind(), _dec3 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(BadgeIcon, _PureComponent);

  var _super = _createSuper(BadgeIcon);

  function BadgeIcon() {
    var _this;

    _classCallCheck(this, BadgeIcon);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      file: []
    };
    return _this;
  }

  _createClass(BadgeIcon, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'userMessage/updateState',
        payload: {
          messageDetail: {}
        }
      });
      this.getMessageDetail();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _prevProps$match$para = prevProps.match.params,
          _prevProps$match$para2 = _prevProps$match$para.type,
          prevType = _prevProps$match$para2 === void 0 ? 'message' : _prevProps$match$para2,
          prevUserMessageId = _prevProps$match$para.userMessageId;
      var _this$props$match$par = this.props.match.params,
          _this$props$match$par2 = _this$props$match$par.type,
          type = _this$props$match$par2 === void 0 ? 'message' : _this$props$match$par2,
          userMessageId = _this$props$match$par.userMessageId;

      if (type !== prevType || userMessageId !== prevUserMessageId) {
        // 需要更新详情
        this.getMessageDetail();
      }
    }
    /**
     * 获取消息详情
     */

  }, {
    key: "getMessageDetail",
    value: function getMessageDetail() {
      var _this2 = this;

      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          match = _this$props.match,
          organizationId = _this$props.organizationId;
      var _match$params = match.params,
          userMessageId = _match$params.userMessageId,
          _match$params$type = _match$params.type,
          type = _match$params$type === void 0 ? 'message' : _match$params$type;
      dispatch({
        type: 'userMessage/queryDetail',
        payload: {
          userMessageId: userMessageId,
          organizationId: organizationId,
          type: type
        }
      }).then(function (res) {
        if (res && res.attachmentUuid) {
          dispatch({
            type: 'userMessage/queryFileList',
            payload: {
              attachmentUUID: res.attachmentUuid,
              bucketName: BKT_MSG,
              directory: 'cmsg01'
            }
          }).then(function (response) {
            if (response) {
              _this2.setState({
                file: response
              });
            }
          });
        }
      });
    }
  }, {
    key: "handleSaveMessageId",
    value: function handleSaveMessageId() {
      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          userMessageId = _this$props2.match.params.userMessageId;
      dispatch({
        type: 'userMessage/updateState',
        payload: {
          userMessageId: userMessageId
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          _this$props3$userMess = _this$props3.userMessage.messageDetail,
          messageDetail = _this$props3$userMess === void 0 ? {} : _this$props3$userMess,
          _this$props3$queryDet = _this$props3.queryDetailLoading,
          queryDetailLoading = _this$props3$queryDet === void 0 ? false : _this$props3$queryDet,
          _this$props3$match$pa = _this$props3.match.params.type,
          type = _this$props3$match$pa === void 0 ? 'message' : _this$props3$match$pa;
      var file = this.state.file;
      var title = type === 'announce' ? messageDetail.title : messageDetail.subject;
      var datetime = dateTimeRender(type === 'announce' ? messageDetail.publishedDate : messageDetail.creationDate);
      var content = type === 'announce' ? messageDetail.noticeBody : messageDetail.content;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cmsg.userMessage.view.message.title.detail').d('消息详情'),
        backPath: "/cmsg/user-message/list"
      }), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(Spin, {
        spinning: queryDetailLoading
      }, /*#__PURE__*/React.createElement("div", {
        style: {
          borderBottom: 'solid 1px #e8e8e8'
        }
      }, /*#__PURE__*/React.createElement("p", {
        style: {
          margin: 0,
          fontWeight: 'bold',
          fontSize: 20
        }
      }, title), /*#__PURE__*/React.createElement("p", null, datetime)), /*#__PURE__*/React.createElement("div", {
        className: styles.content,
        onClick: this.handleSaveMessageId,
        dangerouslySetInnerHTML: {
          __html: content
        }
      }), file.length > 0 && /*#__PURE__*/React.createElement("div", {
        className: styles.upload
      }, /*#__PURE__*/React.createElement(Divider, {
        dashed: true,
        style: {
          margin: '10px 0'
        }
      }), /*#__PURE__*/React.createElement(UploadModal, {
        attachmentUUID: messageDetail.attachmentUuid,
        bucketName: BKT_MSG,
        bucketDirectory: "cmsg01",
        viewOnly: true
      })))));
    }
  }]);

  return BadgeIcon;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "getMessageDetail", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "getMessageDetail"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSaveMessageId", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSaveMessageId"), _class2.prototype)), _class2)) || _class);
export { BadgeIcon as default };